package com.magicode.portal.service.impl;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Entity;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.entiy.base.WebSiteBase;
import com.magicode.cms.entiy.base.WsbeClick;
import com.magicode.cms.entiy.base.WsbeComment;
import com.magicode.cms.entiy.base.WsbeCommentItem;
import com.magicode.cms.entiy.base.WsbeScore;
import com.magicode.cms.entiy.base.WsbeScoreItem;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.common.dao.PageResults;
import com.magicode.portal.service.IWsbeClickService;

@Service("webClickService")
@Transactional
public class WsbeClickServiceImpl implements IWsbeClickService  {

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;

	@Override
	public WsbeClick createByNewsId(HttpSession session, String entityClassName, String newsId, User user) throws UnsupportedEncodingException {
		WsbeClick news = (WsbeClick) this.getWsbeClick(newsId);
		if(null==news){
			news=new  WsbeClick();
		}
		if(news.getId() == null){
			news.setEntityClassName(entityClassName);
			news.setCreateTime(new Date());
			news.setPhoneClickCount(new Long(0));
			news.setClickCount(new Long(0));
			news.setWsbeId(newsId);
			this.commonDaoService.save(news);
			
		}
		Long phonesize=this.getphonesize(newsId);
		phonesize=null==phonesize?0:phonesize+1;
		news.setPhoneClickCount(phonesize);
		Long size=this.getsize(newsId);
		size=null==size?0:size+1;
		news.setClickCount(size);
		this.commonDaoService.update(news);
		try {
			System.out.println("entityClassName的值为："+entityClassName);
			Class cl = Class.forName(entityClassName);
			Object object = cl.newInstance();
			if(cl.getSuperclass().getName().equals(News.class.getName())){
				News siteBase =(News) this.commonDaoService.get(News.class, newsId);
				siteBase.setClickCount(size);
				this.commonDaoService.update(siteBase);
			}
			if(cl.getSuperclass().getName().equals(WebSiteBase.class.getName())){
				String classTable = "";
				if (cl.isAnnotationPresent(Entity.class)) { //是否Entity
					Entity entity = (Entity) cl.getAnnotation(Entity.class);
					classTable=entity.name();		
				}
				String Sql=" update "+classTable+" set click_count="+size+" where id='"+newsId+"' ";
				this.commonDaoService.querySql(Sql, null);	
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return news;
	}
	
	@Override
	public Long getphonesize(String newsId) {
		String hql="select sum(t.phoneClickCount) from "+WsbeClick.class.getName()+" t where t.wsbeId='"+newsId+"'";
		return commonDaoService.countByHqlMap(hql, null);
	}
	
	@Override
	public Long getsize(String newsId) {
		String hql="select sum(t.clickCount) from "+WsbeClick.class.getName()+" t where t.wsbeId='"+newsId+"'";
		return commonDaoService.countByHqlMap(hql, null);
	}
	
	@Override
	public WsbeClick getWsbeClick(String newsId) {
		String hql = " from "+WsbeClick.class.getName()+" t where t.wsbeId='"+newsId+"'";
		return (WsbeClick) commonDaoService.getByHQL(hql,null);
	}
	
}
